// pages/detail/detail.js
const app = getApp();
var utils = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: {
      
    },
    start: true,
    active: [],
    activeList: [],
    list: [],
    font: '展开全部',
    icon: '/images/detail/icon9.png',
    getmore: true,
    listHeight:0,
    allListHeight:0
  },
  //关注
  star() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var sucFunc = function (that, res) {
      if(res.data == 200){
        that.setData({
          start: !that.data.start
        })
      }else{
        wx.showModal({
          title: '提示',
          content: '网络错误，请稍后重试！',
          success: function (res) {
            return
          }
        })
      }
      
      wx.hideLoading();
    }
    var data = { openid: app.d.openid, merchant_id: that.data.product.id }
    utils.wxRequest(that, app.d.path, "Guide/keep", data, sucFunc);
    
  },
  //展开更多
  getmoreFn() {
    this.setData({
      getmore: !this.data.getmore
    })
    if (this.data.getmore == true) {
      var animation = wx.createAnimation({
        duration: 400,
        timingFunction: 'ease',
        delay: 0
      });
      
      animation.height(this.data.listHeight).step()
      this.setData({
        ani: animation.export()
      })
      this.setData({
        list: this.data.active,
        font: '收起'
      })

      this.setData({
        list: this.data.activeList,
        font: '展开全部'
      })
    } else {
      var animation = wx.createAnimation({
        duration: 400,
        timingFunction: 'ease',
        delay: 0
      });
      animation.height(this.data.allListHeight).step()
      this.setData({
        ani: animation.export()
      })
      this.setData({
        list: this.data.active,
        font: '收起'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    that.setData({
      imgPath: app.d.imgPath
    })
    var id = options.id;
    var sucFunc = function (that, res) {
      that.setData({
        product: res.data.res,
        start: res.data.star,
        active:res.data.activity
      })
      WxParse.wxParse('article', 'html', res.data.res.content, that, 5)
      //获取数据前两条展示
      let arr = [];
      for (let i = 0; i < that.data.active.length; i++) {
        if (i < 2) {
          arr.push(that.data.active[i])
        }
      }
      that.setData({
        activeList: arr,
        list: arr
      })
      //适配
      var systemInfo = wx.getSystemInfoSync();
      var height = 320 / 750 * systemInfo.windowWidth;
      var allheight = (160 * that.data.active.length) / 750 * systemInfo.windowWidth;
      that.setData({
        listHeight: height,
        allListHeight: allheight
      })
      
      wx.hideLoading();
      
    }
    var data = { id: id, openid: app.d.openid }
    utils.wxRequest(that, app.d.path, "Guide/detail", data, sucFunc);
    
  },
  phone:function(){
    wx.makePhoneCall({
      phoneNumber: this.data.product.phone,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})